<?php
class Asset extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('asset_model');
		$this->load->model('voucher/fund_model','fund');
	}
	
	public function index($fund_id)
	{
		$user = login_data();
		$data['fund'] = $this->fund->get_row($fund_id);
		$agency_id = (level_data('view'))?(isset($_GET['agency_id']))?$_GET['agency_id']:$user['agency_id']:$user['agency_id'];	
		$where = '';
		if(!empty($_GET['search']))
		{
			$where .= ' AND ASSETS.TITLE like \'%'.$_GET['search'].'%\' ';
		}
		if(!empty($agency_id))
		{
			$where .= ' AND ASSETS.AGENCY_ID = '.$agency_id;
		}
		
		$data['agency_id'] = $agency_id;
		$sql = "SELECT 
			ASSETS.*,
			ASSET_CLASS.ASSET_CLASS_NAME 
		FROM ASSETS
		JOIN ASSET_CLASS ON ASSET_CLASS.ID = ASSETS.ASSET_CLASS_ID
		WHERE ASSETS.FUND_ID = $fund_id
		AND DATE_RECORD <= '".date('Y-m-d')."'
		$where
		ORDER BY ASSETS.ASSET_CLASS_ID ASC, ASSETS.DATE_RECORD ASC";
		$data['result'] = $this->asset_model->get($sql);
		$data['pagination'] = $this->asset_model->pagination();
		$this->template->build('index', $data);
	}
	
	public function form($id = NULL)
	{
		$data['rs'] = $this->asset_model->get_row($id);
		$this->template->append_metadata(js_datepicker());
		$this->template->build('form', $data);
	}
	
	public function save()
	{
		if($_POST)
		{
			$_POST['date_record'] = date_to_mysql($_POST['date_record'], TRUE);
			$this->asset_model->save($_POST);
			set_notify('success', lang('save_data_complete'));
		}
		redirect('asset');
	}
	
	public function delete($id)
	{
		$this->asset_model->delete($id);
		set_notify('success', lang('delete_data_complete'));
		redirect('asset');
	}
	
	public function view($id)
	{
		$data['money_type'] = array('1'=>'เงิน','2'=>'เงินนอกงบประมาณ','3'=>'เงินบริจาค/เงิน','4'=>'อื่นๆ');
		$data['acquire'] = array('1'=>'ตกลง','2'=>'สอบราคา','3'=>'ประกวดราคา','4'=>'วิธีพิเศษ','5'=>'รับ');
		$sql = 'SELECT 
			ASSETS.*, 
			ASSET_CLASS.ASSET_CLASS_NAME, 
			FUNDS.NAME AS FUND_NAME,
			AGENCIES.NAME AS AGENCY_NAME
		FROM ASSETS
		JOIN ASSET_CLASS ON ASSET_CLASS.ID = ASSETS.ASSET_CLASS_ID
		JOIN FUNDS ON FUNDS.ID = ASSETS.FUND_ID
		JOIN AGENCIES ON AGENCIES.ID = ASSETS.AGENCY_ID
		WHERE ASSETS.ID = ?';
		$data['rs'] = $this->db->getrow($sql, $id);
		dbConvert($data['rs']);
		$this->template->build('view', $data);
	}
}
?>